<?php
require_once APPPATH.'third_party/base_controller'.EXT;
    class Cart extends Base_Controller
    {
        function __construct()
        {
            parent::__construct();     
              
            
              
        }
        function index()
        {
            $data = array();
            
            $data['product'] = $this->product_model->show_product();
            $this->load->view('butbi/cart/show',$data);
            
        }
        function views()
		{
		  
			$data['total'] = $this->cart->total_items();//lay so san pham mua
			$data['subtotal'] = $this->cart->total();
            $data['cart'] = $this->cart->contents();             
            $data['title'] = "Gio hang";	
			$this->load_render($this->load->view('butbi/cart/views',$data,TRUE));
		}
	    function add_ajax()
        {
          $data = array(); 
		  $id = $this->input->get('id_product');          
          $qty = $this->input->get('qty');  
          $items = $this->product_model->cate_get($id);
         $getshop=$this->cart->contents();
         
            $data['pro_qty'] = 1;
            foreach($getshop as $item)
            {
                if($item['id'] == $items->id_product)
                {
                    $data['pro_qty'] = $item['qty']+1;
                }
            }
            $shop = array(
               'id'      => $items->id_product,
               'qty'     => $data['pro_qty'],
               'price'   => $items->price,
               'name'    => $items->url,
               'image' => $items->image_thumb               
            );
            if($this->cart->insert($shop))
            {
                $data  = array(
                    'error' => 'Thêm thành công',
                    'total' => $this->cart->total_items()
                );
            } 
            echo json_encode($data);
         
        // redirect('gio-hang');
          
		}
		function add_cart_item()
        {
          $data = array(); 
		  $id = $this->input->post('id_product');          
          $qty = $this->input->post('qty');  
          $items = $this->product_model->cate_get($id);
         $getshop=$this->cart->contents();
         
            $data['pro_qty'] = 1;
            foreach($getshop as $item)
            {
                if($item['id'] == $items->id_product)
                {
                    $data['pro_qty'] = $item['qty']+1;
                }
            }
            $shop = array(
               'id'      => $items->id_product,
               'qty'     => $data['pro_qty'],
               'price'   => $items->price,
               'name'    => $items->url,
               'image' => $items->image_thumb               
            );
            $this->cart->insert($shop);
            
               redirect('gio-hang'); 
            
        // 
          
		}
		
        function show()
        {
           
            $data = array();
            $data['total'] = $this->cart->total_items();//lay so san pham mua
			$data['subtotal'] = $this->cart->total();
            $data['cart'] = $this->cart->contents();             
            $data['title'] = "Gio hang";	
            $this->load->view('cart/show_iterm',$data);
        }
        
        function update()
		{
		
		// Lay tong so san pham
		$count = $this->cart->total_items();
		
		$_qty = $this->input->post('qty');
        
		$_rowid = $this->input->post('rowid');
		
		for($i = 1; $i <= $count; $i++)
		{
		  $item = $this->input->post($i);
            $data = array(
                    'rowid'    => $item['rowid'],
                    'qty'    => $item['qty']
                        );
            $this->cart->update($data); 
            
		} //End for
	
	     redirect('gio-hang');
		
		} // End function update()
		function remove($rowid)
		{
		  
			$this->cart->update(array(
			'rowid' =>$rowid,
			'qty' =>0
			));
			 redirect('gio-hang');
		}
        function destroy()
		{
			$this->cart->destroy();
			redirect('gio-hang');
		}
		function xuly()
        {
            
            $setting = $this->setting_model->get_setting();
            $name = $this->input->get('name');
            $address = $this->input->get('address');
            $phone = $this->input->get('phone');
            $email = $this->input->get('email');
            $comment = $this->input->get('comment');
            $capcha_code = $this->input->get('capcha_code');
            $smtp_secure = "ssl";
            $smtp_server = "smtp.gmail.com";
            $smtp_port = $setting->smtp_port;
            $smtp_username = $setting->smtp_user;
            $smtp_password = $setting->smtp_pass;
            $cart=$this->cart->contents();
            $html = "";
            $html .= "<table style='width:647px;border:1px solid blue;float:left;margin:5px;color:#000;margin-top:5px;'>";
            $html .= "<tr style='background:red; color:white'>";
            $html .="<td>Tên</td>";
            $html .="<td>Số lượng</td>";
            $html .= "<td>Đơn giá</td>";
            $html .= "<td>Thành tiền</td>";
            $html .= "</tr>";
            foreach($cart as $item)
            {
                $html .="<tr style='border-bottom:1px solid #ccc'>";                
                $html .= "<td>".$item['name']."</td>";
                $html .= "<td>".$item['qty']."</td>";
                $html .= "<td>".number_format($item['price'],0,'','.')."</td>";
                $html .= "<td>".number_format($item['price']*$item['qty'],0,'','.')."</td></tr>";
                
            }
            $html .= "<tr><td colspan='5'><strong>Tổng thành tiền:".number_format($this->cart->total(),0,'','.')."VNĐ</strong></td></tr>";
             $html .= "</table><br>";     
            
            
            $html .= "Họ và tên: <b>$name</b><br>";
            $html .= "Email: <b>$email</b><br>";
            $html .= "Phone: <b>$phone</b><br>";
            $html .= "Địa chỉ: <b>$address</b><br>";
            $html .= "Comment: <b>$comment</b><br>";
            
            
            
           
            
            
            include_once $_SERVER['DOCUMENT_ROOT'] . '/public/securimage/securimage.php';
			$securimage = new Securimage();
            if ($securimage->check($capcha_code) == false) {
                $error = "Capcha nhập vào không đúng";
            } else {                
                   
                    $config['email_recevie'] = $setting->email_company;
                    $config['sitename'] = $setting->title_tv;
                    include_once $_SERVER['DOCUMENT_ROOT'] . '/public/class_email/class.phpmailer.php';
					
                    $mail = new PHPMailer(true);
					$mail->IsSMTP();					
					$mail->SMTPDebug  = 1;
					$mail->SMTPAuth   = true;
					$mail->SMTPSecure = $smtp_secure;
					$mail->Host       = $smtp_server;
					$mail->Port       = $smtp_port;
					$mail->Username   = $smtp_username;
					$mail->Password   = $smtp_password;
					$mail->CharSet = 'UTF-8';;
					$mail->AddReplyTo($email, $name);
					$mail->AddAddress($config['email_recevie'], $config['sitename']);
					$mail->SetFrom($email, $name);
					$mail->Subject = 'Liên hệ từ '.$config['sitename'];
					$mail->AltBody = 'Liên hệ từ '.$config['sitename'];
					$mail->MsgHTML($html);			
                    if(!$mail->Send()) {
                      $error = "Mailer Error: " . $mail->ErrorInfo;
                    } else {
                      $error =  "Chúng tôi sẽ phản hồi nhanh nhất. Chân thành cảm ơn quý khách.";
                      $this->cart->destroy();  
                    }
                    $process=1;
            }
            $data = array(
                'status' => $error
            );
            echo json_encode($data);
        }
		function checkout()
		{
		  $data['title'] =  "Thanh toán";
            $data['cart'] = $this->cart->contents();
			$this->load_render($this->load->view('butbi/cart/checkout',$data,TRUE));
		}
    }
?>